package com.sihai.ehr.web.action.sign;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.*;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.AdministrationEvectionCostApplyFormService;
import com.sihai.ehr.service.UserService;
import com.sihai.ehr.utils.DateUtil;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wxn
 * @since 13-4-15 21:22
 *
 * 行政签核系统 - 出差报告单Action
 */
@Results({
        @Result(name = "list", location = "/WEB-INF/pages/sign/administration_evection_cost_apply_form_list.jsp"),
        @Result(name = "add", location = "/WEB-INF/pages/sign/administration_evection_cost_apply_form_input.jsp"),
        @Result(name = "audit", location = "administration_evection_cost_apply_form_audit.jsp")
})
public class AdministrationEvectionCostApplyFormAction extends SignBaseAction {
    @BeanInject
    private AdministrationEvectionCostApplyFormService administrationEvectionCostApplyFormService;
    @BeanInject
    private UserService userService;

    private LimitedList<AdministrationEvectionCostApplyForm> applyForms;
    private AdministrationEvectionCostApplyForm applyForm;
    private List<AdministrationEvectionCostApplyFormDetail> formDetails;

    private List<List<AdministrationEvectionCostApplyFormAuditing>> audits;

    @Action("/public/administration-evection-cost-apply-form")
    public String listByUser() throws Exception {
        userType = 2;
        String temp = execute();
        showChildOrg = false;
        return temp;
    }

    @Action("/sign/administration-evection-cost-apply-form")
    @Override
    public String execute() throws Exception {
        showChildOrg = true;
        init();
        User user = null;
        if(userType == 2){
            user = userService.getUser(getCurrentUserId());
        }
        applyForms = administrationEvectionCostApplyFormService.findAdministrationEvectionCostApplyForm(user, searchFormPojo, createLimit(), createSorter());
        this.paging(applyForms);
        this.setPageUrl(userType == 1 ? "sign/administration-evection-cost-apply-form.xhtml" : "public/administration-evection-cost-apply-form.xhtml");
        return "list";
    }

    @Action("/public/administration-evection-cost-apply-form-input")
    public String toAddByUser() throws Exception {
        userType = 2;
        return "add";
    }

    @Action("/sign/administration-evection-cost-apply-form-input")
    public String toAdd() throws Exception {
        return "add";
    }

    @Action("/public/administration-evection-cost-apply-form-save")
    public String saveByUser() throws Exception {
        userType = 2;
        return save();
    }

    //保存
    @Action("/sign/administration-evection-cost-apply-form-save")
    public String save() throws Exception {
        String[] detailDates = getParameterValues("detail_evectionDate_input");
        String[] detailReasons = getParameterValues("detail_evectionReason_input");
        String[] detailPlaneCosts = getParameterValues("detail_planeCost_input");
        String[] detailTrafficCosts = getParameterValues("detail_trafficCost_input");
        String[] detailStayCosts = getParameterValues("detail_stayCost_input");
        String[] detailMealCosts = getParameterValues("detail_mealCost_input");
        String[] detailOtherCosts = getParameterValues("detail_otherCost_input");
        if(applyForm == null || detailDates == null ||  detailReasons == null || detailPlaneCosts == null || detailTrafficCosts == null || detailStayCosts == null || detailMealCosts == null || detailOtherCosts == null){
            return execute();
        }
        int length = detailDates.length;
        List<AdministrationEvectionCostApplyFormDetail> details = new ArrayList<AdministrationEvectionCostApplyFormDetail>(length);
        Float total = 0f;
        for(int i=0; i<length; i++){
            AdministrationEvectionCostApplyFormDetail detail = new AdministrationEvectionCostApplyFormDetail();
            detail.setEvectionDate(DateUtil.createDate(detailDates[i], "yyyy-MM-dd"));
            detail.setEvectionReason(detailReasons[i]);
            detail.setPlaneCost(NumberUtils.createFloat(detailPlaneCosts[i]));
            detail.setTrafficCost(NumberUtils.createFloat(detailTrafficCosts[i]));
            detail.setStayCost(NumberUtils.createFloat(detailStayCosts[i]));
            detail.setMealCost(NumberUtils.createFloat(detailMealCosts[i]));
            detail.setOtherCost(NumberUtils.createFloat(detailOtherCosts[i]));
            detail.setAmount(detail.getPlaneCost()+detail.getTrafficCost()+detail.getStayCost()+detail.getMealCost()+detail.getOtherCost());
            details.add(detail);
            total += detail.getAmount();
        }
        applyForm.setAmount(total);

        Long userId = getCurrentUserId();
        EmployeeinfoWork work = null;
        if(userId > 0){
            User user = userService.getUser(userId);
            if(user != null){
                work = user.getWork();
            }
        }
        applyForm.setStatus(AuditStatus.wait_audit);
        applyForm.setWork(work);
        administrationEvectionCostApplyFormService.save(applyForm, details);
        return execute();
    }

    @Action("/sign/administration-evection-cost-apply-form-audit")
    public String toAudit() throws Exception {
        //得到审核对象
        if(applyForm == null || applyForm.getId() == null){
            return execute();
        }
        applyForm = administrationEvectionCostApplyFormService.get(applyForm.getId());
        formDetails = administrationEvectionCostApplyFormService.findAdministrationEvectionCostApplyFormDetails(applyForm);

        //得到当前审核历史记录(没有就先保存一个审核流程次数)
        audits = administrationEvectionCostApplyFormService.findAdministrationEvectionCostApplyFormAuditing(applyForm.getId());
        if(audits == null || audits.isEmpty()){
            administrationEvectionCostApplyFormService.saveAdministrationEvectionCostApplyFormAuditingProcess(applyForm.getId());
            audits = administrationEvectionCostApplyFormService.findAdministrationEvectionCostApplyFormAuditing(applyForm.getId());
        }
        //检查当前登录用户是否可以审核当前结点
        if(applyForm.getWork() != null && audits != null && !audits.isEmpty()){
            checkCanAudit(applyForm.getWork().getDuty(), audits.get(audits.size()-1));
        }
        currentAuditType = AuditType.audit_administration_evection_cost_report_form.toString();

        return "audit";
    }

    public LimitedList<AdministrationEvectionCostApplyForm> getApplyForms() {
        return applyForms;
    }

    public AdministrationEvectionCostApplyForm getApplyForm() {
        return applyForm;
    }

    public void setApplyForm(AdministrationEvectionCostApplyForm applyForm) {
        this.applyForm = applyForm;
    }

    public List<List<AdministrationEvectionCostApplyFormAuditing>> getAudits() {
        return audits;
    }

    public List<AdministrationEvectionCostApplyFormDetail> getFormDetails() {
        return formDetails;
    }
}
